import flixel.FlxSprite;
import flixel.group.FlxGroup;
import flixel.text.FlxText;
import flixel.FlxG;

class MessageBoard extends FlxGroup{
	private static inline var percent_width: Float = 0.8;	// ancho: porcentaje respecto del ancho de la camara
	private static inline var percent_height: Float = 0.2;	// alto: porcentaje respecto del alto de la camara
	private static inline var y_percent: Float = 0.1;		// posición en y del mensaje: porcentaje respecto al alto de la camara
	private static inline var text_padding: Int = 10;
	private static inline var fadeTime: Float = 1;
	private var text: FlxText;
	private var background: FlxSprite;
	private var msgTime: Float;
	public static var instance: MessageBoard = null;


	public function new(){
		super();
		// tamaño y posición del cuadrado de fondo 
		var width = FlxG.camera.width*percent_width;
		var height = FlxG.camera.height*percent_height;
		var x = (FlxG.camera.width-width) / 2;
		background = new FlxSprite();
		background.scrollFactor.x = 0;
		background.scrollFactor.y = 0;
		background.x = x;
		background.y = FlxG.camera.height * y_percent;
		

		text = new FlxText( x + text_padding , background.y + text_padding, Std.int(width) - text_padding * 2 , "", 10);
		text.scrollFactor.x = 0;
		text.scrollFactor.y = 0;
		text.size = 10;
		text.antialiasing = false;
		text.alignment = "center";
		add(background);
		add(text);
		msgTime = 0;
		instance = this;
	}

	public override function update(){
		super.update();
		msgTime -= FlxG.elapsed;
		if(msgTime <= fadeTime){
			var alpha = 1 - ((fadeTime - msgTime)/fadeTime);
			text.alpha = alpha;
			background.alpha = 0.5 * alpha;
		}else if (msgTime <= 0){
			text.visible = false;
			background.visible = false;
		}
	}

	public static function showMessage(msg: String)
	{
		instance.show(msg);
	}

	public function show(msg: String, time: Float = 5){
		text.text = msg;
		text.visible = true;
		text.alpha = 1;
		background.visible = true;
		background.alpha = 0.5;

		var width = FlxG.camera.width*percent_width;
		var height = FlxG.camera.height*percent_height;
		background.makeGraphic(Std.int(width), Std.int(text.height + text_padding * 6), 0xFF000000);
		msgTime = time;
	}
}